package com.fuxinglianbao.controllers;

import com.fuxinglianbao.models.PaymentOrder;
import com.fuxinglianbao.models.WithdrawRecord;
import com.fuxinglianbao.services.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/create")
    public ResponseEntity<?> createPayment(@RequestParam Long userId,
                                         @RequestParam BigDecimal amount,
                                         @RequestParam PaymentOrder.PaymentType paymentType,
                                         @RequestParam String notifyUrl,
                                         @RequestParam String returnUrl) {
        PaymentOrder order = paymentService.createPaymentOrder(userId, amount, paymentType, notifyUrl, returnUrl);
        Map<String, Object> response = new HashMap<>();
        response.put("orderNo", order.getOrderNo());
        response.put("amount", order.getAmount());
        response.put("status", order.getStatus());
        return ResponseEntity.ok(response);
    }

    @PostMapping("/notify/{paymentType}")
    public ResponseEntity<?> handlePaymentNotify(@PathVariable PaymentOrder.PaymentType paymentType,
                                               @RequestParam String orderNo,
                                               @RequestParam String transactionId,
                                               @RequestParam PaymentOrder.PaymentStatus status) {
        paymentService.handlePaymentNotify(orderNo, transactionId, status);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/order/{orderNo}")
    public ResponseEntity<?> getPaymentOrder(@PathVariable String orderNo) {
        PaymentOrder order = paymentService.getPaymentOrder(orderNo);
        if (order != null) {
            return ResponseEntity.ok(order);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping("/withdraw")
    public ResponseEntity<?> createWithdraw(@RequestParam Long userId,
                                          @RequestParam BigDecimal amount,
                                          @RequestParam WithdrawRecord.WithdrawType withdrawType,
                                          @RequestParam String accountInfo) {
        WithdrawRecord record = paymentService.createWithdrawRequest(userId, amount, withdrawType, accountInfo);
        Map<String, Object> response = new HashMap<>();
        response.put("withdrawNo", record.getWithdrawNo());
        response.put("amount", record.getAmount());
        response.put("status", record.getStatus());
        return ResponseEntity.ok(response);
    }

    @PostMapping("/withdraw/process")
    public ResponseEntity<?> processWithdraw(@RequestParam String withdrawNo,
                                           @RequestParam WithdrawRecord.WithdrawStatus status,
                                           @RequestParam(required = false) String remark) {
        paymentService.processWithdrawRequest(withdrawNo, status, remark);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/withdraw/{withdrawNo}")
    public ResponseEntity<?> getWithdrawRecord(@PathVariable String withdrawNo) {
        WithdrawRecord record = paymentService.getWithdrawRecord(withdrawNo);
        if (record != null) {
            return ResponseEntity.ok(record);
        }
        return ResponseEntity.notFound().build();
    }
}